<?php
// $Id: index.php,v 2.10.1 2005/03/25 18:05:00 Yoshi.Sakai Exp $

require('header.php');
require_once 'PopnupBlogUtils.php';

/*
$param = isset($HTTP_GET_VARS['param']) ? intval($HTTP_GET_VARS['param']) : 0;
if($param == 0){
	redirect_header(XOOPS_URL.'/',1,_MD_POPNUPBLOG_INTERNALERROR);
	exit();
}
*/
$params = PopnupBlogUtils::getDateFromHttpParams();
$start = PopnupBlogUtils::getStartFromHttpParams();
$view = $BlogCNF['default_view'];
//print_r($params);
//echo $start;
if($params){
	$xoopsTpl->assign('popimg',PopnupBlogUtils::mail_popimg());		// get email
	$blog = new PopnupBlog($params['blogid']);
	if(PopnupBlogUtils::isCompleteDate($params) && ($params['command']=='tb') ){
		$tb = $blog->useTrackBack();
		if($tb == false){
			redirect_header(XOOPS_URL.'/',1,_MD_POPNUPBLOG_INTERNALERROR);
			exit();
		}
		$result = $blog->getBlogData($params['year'], $params['month'], $params['date'], $params['hours'], $params['minutes'], $params['seconds']);
		$xoopsOption['template_main'] = 'popnupblog_trackback.html';
		$xoopsTpl->assign('url', PopnupBlogUtils::createUrl($params['blogid'], $params['year'], $params['month'], $params['date'], $params['hours'], $params['minutes'], $params['seconds']));
		$xoopsTpl->assign('mt_tb_url', PopnupBlogUtils::makeTrackBackURL($params['blogid'], $params['year'], $params['month'], $params['date'], $params['hours'], $params['minutes'], $params['seconds']));
		$xoopsTpl->assign('xoops_module_header', '<link rel="alternate" type="application/rss+xml" title="RSS" href="'.PopnupBlogUtils::createRssURL($params['blogid']).'">');
	}else{
		// init 	
		$xoopsTpl->assign('popnupblog_editable', false);
		$xoopsTpl->assign('popnupblog_commentable', false);
		$xoopsTpl->assign('params', $params['params']);

		$result = array();
		$result_max_num = (
			PopnupBlogUtils::isCompleteDate($params) && array_key_exists('month', $params) ) ? 31 : POPNUPBLOG_VIEW_LIST_NUM;
		$vote = isset($params['vote']) ? $params['vote'] : 0;
		
		$result = $blog->getBlogData($params['year'], $params['month'], $params['date'], $params['hours'], $params['minutes'], $params['seconds'], $params['command'], $start,$vote);

		if(!empty($result['blog']))
			$xoopsTpl->assign('popnupblog_blogdata', $result['blog']);
		if (!empty($xoopsUser))
			$xoopsTpl->assign('popnupblog_admin', $xoopsUser->isAdmin());
		$xoopsTpl->assign('popnupblog', $result);
		$xoopsTpl->assign('blog_title', $blog->getTitle());
		$xoopsTpl->assign('blog_desc', $blog->getBlogdesc());
		$xoopsTpl->assign('blog_uname', $blog->getTargetUname());
		$xoopsTpl->assign('popnupblog_targetUid', isset($params['uid']) ? $params['uid'] : 0);
		$xoopsTpl->assign('popnupblog_targetBid', $params['blogid']);
		$xoopsTpl->assign('popnupblog_index', $blog->getBlogIndex());
		if(!empty($result['jump_url']))
			$xoopsTpl->assign('jump_url', $result['jump_url']);
		if(!empty($result['today']))
			$xoopsTpl->assign('popnupblog_today', $result['today']);
		if($blog->isPublic()){
			$xoopsTpl->assign('popnupblog_user_rss', PopnupBlogUtils::createRssURL($params['blogid']));
		}
		if (!empty($xoopsUser)){
			if($blog->canWrite($params['blogid']) || $xoopsUser->isAdmin()){
				$xoopsTpl->assign('popnupblog_editable', true);
			}
		}
		$show_name = PopnupBlogUtils::getXoopsModuleConfig('show_name');
		if ($xoopsUser){
			if ( $show_name==1 && (trim(PopnupBlogUtils::username($xoopsUser->uid()))!='') )
				$uname = PopnupBlogUtils::username($xoopsUser->uid());
			else
				$uname = $xoopsUser->uname();
		}
		if($blog->canComment($params['blogid'])){
			$xoopsTpl->assign('popnupblog_commentable', true);
			if($xoopsUser){
				$xoopsTpl->assign('popnupblog_uid', $xoopsUser->uid());
				$xoopsTpl->assign('popnupblog_uname', $uname);
			}
		}
		if($blog->canVote($params['blogid'])){
			$xoopsTpl->assign('popnupblog_votable', true);
			if($xoopsUser){
				$xoopsTpl->assign('popnupblog_uid', $xoopsUser->uid());
				$xoopsTpl->assign('popnupblog_uname', $uname);
			}
		}
		$blog->recieve_trackback_ping($params);
		if(PopnupBlogUtils::isCompleteDate($params)){
			$xoopsTpl->assign('trackbacks', $blog->getTrackBack($params));
		}
		
		if (isset($HTTP_GET_VARS['view'])) $view = $HTTP_GET_VARS['view'];
		$xoopsOption['template_main'] = 'popnupblog_view.html';
		$mh = '';
		$mh .= '<link rel="alternate" type="application/rss+xml" title="RSS" href="'.PopnupBlogUtils::createRssURL($params['blogid']).'">'."\n";
		$mh .= '<link rel="start" href="'.PopnupBlogUtils::createUrl($params['blogid']).'" title="Home" />'."\n";
		$xoopsTpl->assign('xoops_module_header', $mh);
		// $xoopsTpl->assign('xoops_module_header', '<link rel="alternate" type="application/rss+xml" title="RSS" href="'.PopnupBlogUtils::createRssURL($params['blogid']).'">');
		// <link rel="start" href="http://el30.sub.jp/" title="Home" />
		// $xoopsTpl->assign('popnupblog_home_url', PopnupBlogUtils::createUrl($params['blogid']));
	}
}else{
//	$popimg = PopnupBlogUtils::mail_popimg();
//	print($popimg);
	$xoopsTpl->assign('popimg',PopnupBlogUtils::mail_popimg());		// get email
	$cat_id=0;
	if (isset($HTTP_GET_VARS['cat_id'])) $cat_id = $HTTP_GET_VARS['cat_id'];
	if (isset($HTTP_POST_VARS['cat_id'])) $cat_id = $HTTP_POST_VARS['cat_id'];
	$xoopsTpl->assign('popnupblog', PopnupBlogUtils::get_blog_list($start,$cat_id));
	if (isset($HTTP_GET_VARS['view'])) $view = $HTTP_GET_VARS['view'];
	if (isset($HTTP_POST_VARS['view'])) $view = $HTTP_POST_VARS['view'];
	$xoopsTpl->assign('view',$view);
	$rssUrl = XOOPS_URL.'/modules/popnupblog/rss.php';
	$xoopsTpl->assign('popnupblog_rss_url', $rssUrl);
	$categories = PopnupBlogUtils::get_categories();
	$xoopsTpl->assign('categories', PopnupBlogUtils::mkselect('cat_id',$categories,$cat_id));
	$jump_url = PopnupBlogUtils::mk_list_url($cat_id,$view);
		$xoopsTpl->assign('jump_url', $jump_url);
	$xoopsOption['template_main'] = 'popnupblog_list.html';
	$xoopsTpl->assign('xoops_module_header', '<link rel="alternate" type="application/rss+xml" title="RSS" href="'.$rssUrl.'">');
}


require('footer.php');

?>
